home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MISC / SENDMAIL.ARC / cc / sendmail
Encoding:
Text File  |  1993-10-04  |  4.6 KB  |  226 lines

  1. extern "C" {
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. }
  6. #include "mask.h"
  7.  
  8. void usage()
  9. {
  10.   fprintf( stderr, "Usage: sendmail [-f file] [-C file] [user1] [user2] ...\n" );
  11.   exit( 1 );
  12. }
  13.  
  14.  
  15. #define BUFFLEN 512
  16.  
  17. FILE *infile = stdin;
  18. FILE *config = NULL;
  19. FILE *temp = NULL;
  20. char to[256], fileto[256];
  21. char mailfile[256];
  22. char buff[BUFFLEN];
  23.  
  24.  
  25. void quit( int code )
  26. {
  27.   if( infile ) fclose( infile );
  28.   if( config ) fclose( config );
  29.   if( temp ) {
  30.     fclose( temp );
  31.     remove( mailfile );
  32.     }
  33.   exit( code );
  34. }
  35.  
  36.  
  37. char *get_fname( int *i, int argc, char **argv )
  38. {
  39.   char *fname;
  40.  
  41.   if( argv[*i][2] ) return &argv[*i][2];
  42.   if( (*i)+1 >= argc ) {
  43.     fprintf( stderr, "sendmail: no file name given to %s option\n", argv[*i] );
  44.     quit( 1 );
  45.     }
  46.   return argv[++(*i)];
  47. }
  48.  
  49.  
  50. void workfile()
  51. {
  52.   int len;
  53.  
  54.   fileto[0] = 0;
  55.   tmpnam( mailfile );
  56.   temp = fopen( mailfile, "w" );
  57.   if( ! temp ) {
  58.     fprintf( stderr, "sendmail: unable to create temporary file\n" );
  59.     quit( 1 );
  60.     }
  61.   while( fgets( buff, BUFFLEN-1, infile )) {
  62.     len = strlen(buff)-1;
  63.     while( buff[len]=='\r' || buff[len]=='\n' && len>=0 ) len--;
  64.     buff[++len] = 0;
  65.     if( ! *fileto && ! memcmp( buff, "To: ", 4 ) ) strcpy( fileto, buff+4 );
  66.     fprintf( temp, "%s\n", buff );
  67.     }
  68.   fclose( temp );
  69. }
  70.  
  71.  
  72. void exe( class mskcmp *mask, char *com )
  73. {
  74.   int i=0;
  75.   char *next;
  76.   char *last=com;
  77.   char command[300];
  78.   char *execomm=command;
  79.   char *ins;
  80.  
  81.   *execomm = 0;
  82.   while( 1 ) {
  83.     next = strchr( last, '%' );
  84.     if( next==0 || ! next[1] ) {
  85.       memcpy( &execomm[i], last, strlen(last)+1 );
  86.       execomm[strlen(last)+1+i] = 0;
  87.       break;
  88.       }
  89.     memcpy( &execomm[i], last, next-last );
  90.     i += next-last;
  91.     switch( next[1] ) {
  92.       case '%':
  93.         execomm[i++] = '%';
  94.         break;
  95.       case 'f':
  96.         memcpy( &execomm[i], mailfile, strlen(mailfile) );
  97.         i += strlen(mailfile);
  98.         break;
  99.       case '0':
  100.       case '1':
  101.       case '2':
  102.       case '3':
  103.       case '4':
  104.       case '5':
  105.       case '6':
  106.       case '7':
  107.       case '8':
  108.       case '9':
  109.         if( ! (ins = mask->getvar( next[1]-'0' ))) break;
  110.         memcpy( &execomm[i], ins, strlen( ins ) );
  111.         i += strlen( ins );
  112.         break;
  113.       case 't':
  114.         memcpy( &execomm[i], to, strlen( to ) );
  115.         i += strlen( to );
  116.         break;
  117.       default:
  118.         memcpy( &execomm[i], next, 2 );
  119.         i += 2;
  120.         break;
  121.       }
  122.     last = next+2;
  123.     }
  124.   if( system(command) ) {
  125.     fprintf( stderr, "sendmail: mail delivery failed\n" );
  126.     exit( 1 );
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. int sendmail()
  133. {
  134.   class mskcmp *mask;
  135.   char *com;
  136.  
  137.   if( ! *to ) {
  138.     fprintf( stderr, "sendmail: mail file corrupt - no destination line\n" );
  139.     quit( 1 );
  140.     }
  141.   fseek( config, 0, SEEK_SET );
  142.   while( com=fgets( buff, BUFFLEN-1, config ) ) if( *buff!='#' ) {
  143.     for( com=buff; *com && *com!=' '; com++ );
  144.     if( ! *com ) continue;
  145.     *(com++) = 0;
  146.     mask = new mskcmp( buff );
  147.     if( ! mask->isvalid( to ) ) {
  148.       exe( mask, com );
  149.       delete mask;
  150.       break;
  151.       }
  152.     delete mask;
  153.     }
  154.   if( ! com ) {
  155.     fprintf( stderr, "sendmail: no mail method for this destination\n" );
  156.     quit( 2 );
  157.     }
  158.   return 0;
  159. }
  160.  
  161.  
  162. int main( int argc, char **argv )
  163. {
  164.   int i;
  165.   char *fname;
  166.  
  167.   for( i=1; i<argc && argv[i][0]=='-'; i++ ) {
  168.     if( argv[i][0] != '-' ) usage();
  169.     switch( argv[i][1] ) {
  170.       case 'f':
  171.     fname = get_fname( &i, argc, argv );
  172.     if( !(infile = fopen( fname, "rb" )) ) {
  173.       fprintf( stderr, "sendmail: file %s not found\n", fname );
  174.       quit( 1 );
  175.       }
  176.     break;
  177.  
  178.       case 'C':
  179.         fname = get_fname( &i, argc, argv );
  180.         if( !(config = fopen( fname, "rb" )) ) {
  181.           fprintf( stderr, "sendmail: configuration file %s not found\n", fname );
  182.           quit( 1 );
  183.           }
  184.         break;
  185.  
  186.       default:
  187.     fprintf( stderr, "Unknown option %s\n", argv[i] );
  188.     usage();
  189.       }
  190.     }
  191.   if( ! config ) {
  192.     if( ! (fname = getenv( "sendmail$config" )) ) fname = "mailers";
  193.     config = fopen( fname, "rb" );
  194.     if( ! config ) {
  195.       fprintf( stderr, "sendmail: configuration file %s not found\n", fname );
  196.       quit( 1 );
  197.       }
  198.     }
  199.   workfile();
  200.   fclose( infile ); infile = NULL;
  201.   if( i<argc ) for( ; i<argc; i++ ) {
  202.     strcpy( to, argv[i] );
  203.     sendmail();
  204.     }
  205.    else {
  206.      char *next, *end;
  207.  
  208.      for( next=fileto; *next; ) {
  209.        while( *next==' ' ) next++;
  210.        if( ! *next ) break;
  211.        end = strchr( next, ',' );
  212.        if( ! end ) {
  213.          end = next+strlen(next);
  214.          end[1] = 0;
  215.          }
  216.        *end = 0;
  217.        strcpy( to, next );
  218.        for( i=strlen(to)-1; to[i]==' '; i-- ) to[i] = 0;    /* 'rtrim' */
  219.        sendmail();
  220.        next = end+1;
  221.        }
  222.      }
  223.   quit( 0 );
  224. }
  225.  
  226.